package com.efg.games.superclick.my
{
    import com.efg.framework.CustomEventLevelScreenUpdate;
    import com.efg.framework.CustomEventScoreBoardUpdate;
    import com.efg.framework.Game;
    import flash.display.DisplayObject;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import com.efg.games.superclick.my.Main;
    
    /**
     * ...
     * @author jingping.shenjp
     */
    public class SuperClick extends Game
    {
        private const BLUE:uint = 0x0000FF;
        private const RED:uint = 0xFF0000;
        private const LIFE:Number = 15;
        
        private var circles:Vector.<Circle>;
        private var scoreTfs:Vector.<ScoreTextField>;
        
        private var level:int = 0;
        private var gameOver:Boolean;
        private var numClicks:int = 0;
        private var numCreated:int = 0;
        private var maxScore:int = 0;
        private var percent:int = 0;
        private var score:int = 0;
        
        private var circleGrowSpeed:Number = 0;
        private var maxCirclesOnScreen:int = 0;
        private var numCircles:Number = 0;
        private var percentNeeded:Number = 0;
        private var percentBadCircles:Number = 0;
        
        public function SuperClick()
        {
        
        }
        
        override public function newGame():void
        {
            super.newGame();
        }
        
        private function initParameter():void
        {
            circles = new Vector.<Circle>();
            scoreTfs = new Vector.<ScoreTextField>();
            score = 0;
            gameOver = false;
            numClicks = 0;
            numCreated = 0;
            circleGrowSpeed = 0.1 * level;
            maxCirclesOnScreen = 10 * level;
            numCircles = 25 * level;
            percentBadCircles = 0.1 * level;
        }
        
        override public function newLevel():void
        {
            super.newLevel();
            clearUp();
            level++;
            initParameter();
            dispatchEvent(new CustomEventLevelScreenUpdate(CustomEventLevelScreenUpdate.UPDATE_TEXT, String(level)));
        }
        
        override public function runGame():void
        {
            super.runGame();
            if (!stage.hasEventListener(MouseEvent.MOUSE_DOWN))
            {
                stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownEvent);
            }
            addCircles();
            updateCircles();
            updateSocreTfs();
            checkforEndGame();
            checkforNewLevel();
        }
        
        private function addCircles():void
        {
            var color:uint;
            var circle:Circle;
            if (circles.length < maxCirclesOnScreen)
            {
                color = (Math.random() < percentBadCircles) ? RED : BLUE;
                circle = new Circle(color, 1, circleGrowSpeed);
                circle.x = 400 * Math.random();
                circle.y = 400 * Math.random();
                addChild(circle);
                circles.push(circle);
                numCreated++;
            }
        }
        
        private function updateCircles():void
        {
            var numloop:int = circles.length;
            var circle:Circle;
            for (var i:int = numloop-1; i >=0 ; i--) 
            {
                circle = circles[i];
                if (circle.width > 50)
                {
                    circle.fadingOut = true;
                }
                circle.update();
                if (circle.isDispose())
                {
                    removeCircle(circle);
                }
            }
        }
        
        private function updateSocreTfs():void 
        {
            var numloop:int = scoreTfs.length;
            var scoreTf:ScoreTextField;
            for (var i:int = numloop-1; i >=0 ; i--) 
            {
                scoreTf = scoreTfs[i];
                if (scoreTf.isDispose())
                {
                   removeScoreTf(scoreTf);
                }
                else
                {
                   scoreTf.update();
                }
            }
        }
        
        private function onMouseDownEvent(e:MouseEvent):void
        {
            var display:DisplayObject = e.target as DisplayObject;
            var circle:Circle;
            var currentScore:int;
            if (display is Circle)
            {
                circle = display as Circle;
                if (circle.color == RED)
                {
                    gameOver = true;
                }
                else
                {
                    currentScore = circle.width * 10;
                    var scoreTf:ScoreTextField = new ScoreTextField(currentScore + "", LIFE);
                    scoreTf.x = circle.x;
                    scoreTf.y = circle.y;
                    addChild(scoreTf);
                    scoreTfs.push(scoreTf);
                    score += currentScore;
                    dispatchEvent(new CustomEventScoreBoardUpdate(CustomEventScoreBoardUpdate.UPDATE_TEXT, Main.SCORE_BOARD_SCORE, score + ""));
                    removeCircle(circle);
                    circle.dispose();
                    numClicks++;
                }
            }
        }
        
        private function removeCircle(circle:Circle):void
        {
            circles.splice(circles.indexOf(circle), 1);
            removeChild(circle);
        }
        
        private function removeScoreTf(scoreTf:ScoreTextField):void 
        {
            scoreTfs.splice(scoreTfs.indexOf(scoreTf), 1);
            removeChild(scoreTf);
        }
        
        private function checkforNewLevel():void 
        {
            if (numCreated > numCircles)
            {
                if (score > 100 * level)
                {
                    dispatchEvent(new Event(NEW_LEVEL));
                }
            }
        }
        
        private function checkforEndGame():void
        {
            if (gameOver)
            {
                level = 0;
                stage.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDownEvent);
                dispatchEvent(new Event(GAME_OVER));
            }
        }
        
        private function clearUp():void 
        {
            if (circles)
            {
                for each (var circle:Circle in circles) 
                {
                    circle.dispose();
                }
                circles.length = 0;
                circles = null;
            }
            if (scoreTfs)
            {
                for each (var scoreTf:ScoreTextField in scoreTfs) 
                {
                    scoreTf.dispose();
                }
                scoreTfs.length = 0;
                scoreTfs = null;
            }
            while (this.numChildren)
            {
                this.removeChildAt(0);
            }
        }
    }

}